
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   
      <meta content="Percussion Rhythmyx" name="generator">
      <title>Java Tip 17: Integrating Java with C++ - Java World</title>
      
      <script language="JavaScript">
			var topicid = 0; 
		    subtopicid = 0; 
		</script><script>
			function HighlightText(divid){
			var code = document.body.createTextRange();
			code.moveToElementText(document.getElementById('codewrap' + divid));
			code.execCommand("SelectAll");
			code.execCommand("Copy");
			} 
		</script><meta name="TopicID" content="1">
      <meta name="SubTopicID" content="57">
      <meta name="channels" content="Java World">
      <meta name="description" content="This article explores some of the issues involved in integrating&#xA;C++ code into a Java program. It shows how to call from a Java&#xA;object to a C++ object, and how to call from a C++ object to a Java&#xA;object. Interaction with garbage collection is explored, and a&#xA;simple framework for integrating Java and C++ is developed.&#xA;Finally, current solutions are compared with what can be expected&#xA;from JDK 1.1, as well as from future offerings. (3,982&#xA;words)">
      <meta name="keywords" content=",Java, magazine, programming, articles,  developers, JavaSoft, JavaWorld, Nuts &amp; Bolts, News &amp; Views, Java Resources">
      <meta name="contenttype" content="News">
      <meta name="publicationDate" content="1996-09-01">
      <meta name="author" content="Bill Foote">
      <meta name="publisher" content="JavaWorld.com">
      
      
      
      
      
      
      
      
      
      <meta http-equiv="expires" content="0" />
<meta http-equiv="Pragma" content="no-cache" />












  



  




<!-- SSI Error -->



<!-- put the # sign in the comment below to go live -->




<style>#reg{BACKGROUND: #ebe7d9;PADDING:5px;}</style>
<script language="javascript">
  var outerref = new String("(none)");
  var nwchannel = 'jw';
  var refresh = 0;
  
  var jq_rxid = '83833';
</script>
<base target = "_top">
<script src="/includes/head-scripts.js"></script>

<!-- HBX -->
<script language="javascript1.1">
var _hbEC=0,_hbE=new Array;function _hbEvent(a,b){b=_hbE[_hbEC++]=new Object();b._N=a;b._C=0;return b;}
var hbx=_hbEvent("pv");hbx.vpc="HBX0103u";hbx.gn="a.javaworld.com";
var doctitle = hbxStrip(document.title);
var loc = new String(document.location.href);
loc = hbxStrip(loc);

hbx.acct="DM54120129AB;DM550210NGWB";
hbx.pn="PUT+PAGE+NAME+HERE";
hbx.mlc="CONTENT+CATEGORY";
hbx.pndef="title";
hbx.ctdef="full";
hbx.lvm="300";
hbx.lidt="txl";

hbx.fv="";//FORM VAL MIN
hbx.lt="auto"; //LINK TR
hbx.dlf="n";//Dl FILTER
hbx.dft="n";//Dl FILE NAMING
hbx.elf="n";//EXIT LINK FILTER

hbx.seg="-1";
hbx.ci="";
hbx.hc1="jw_general|";
hbx.hc2=keywords+"|"+doctitle;
hbx.hc3=author+"|"+doctitle;
hbx.hc4=doctitle+"|"+author;
hbx.pec="";//ERROR CODES
</script><script language="javascript1.1" src="/includes/hbx.js"></script>
<!--END HBX -->

<!-- Start Quantcast tag -->
<script type="text/javascript" src="//secure.quantserve.com/quant.js"></script>
<script type="text/javascript">
_qoptions = {
            tags:"nww"
};
_qacct="p-25K88fxDSEn9Y";quantserve();</script>
<noscript>
<a href="http://www.quantcast.com/p-25K88fxDSEn9Y" target="_blank"><img src="//secure.quantserve.com/pixel/p-25K88fxDSEn9Y.gif?tags=nww" border="0" style="display: none" height="1" width="1"></a></noscript>
<!-- End Quantcast tag -->

<!--
channels: jw
keywords (topics): 
contenttype (tasks): News
site: jw_general
pagename: 
keyval: 
ibsearch: (none)
-->

	 

      <link href="/includes/styles/adstyles.css" rel="stylesheet" type="text/css">
      <link href="/includes/styles/jw-main.css" rel="stylesheet" type="text/css">
   </head>
   <body onload="init()">
      
      <!--stopindex-->
      
      <div id="wrapper">
         <div id="bannerarea">
            <div id="mainbanner">
               



  

<script> bannersrc = "http://ad.doubleclick.net/adi/idg.us.nwf.jw_general/;pos=top;sz=728x90;ptile=1;type=news;ord=023131?";</script>
<!--stopindex-->
<!-- begin ad tag -->
<NOLAYER>
<IFRAME id="banner" src="http://ad.doubleclick.net/adi/idg.us.nwf.jw_general/;pos=top;sz=728x90;ptile=1;type=news;ord=023131?" width="728" height="90" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no">
<A href="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;abr=!ie;pos=top;sz=728x90;ptile=1;type=news;ord=023131?">
<IMG src="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;abr=!ie;pos=top;sz=728x90;ptile=1;type=news;ord=023131?" border="0" width="728" height="90"></A>
</IFRAME>
</NOLAYER>
<ILAYER id="banner" visibility="hidden" width="728" height="90"></ILAYER>
<!-- end ad tag -->


            </div>
            <div class="separator">
               <!-- xxxxxxxxxxxxxx -->
            </div>
            <div id="masthead">
               <div id="logoarea">
                  <a href="http://www.javaworld.com"><img src="/images/jw-logo.gif" alt="JavaWorld" width="201" height="58" border="0" /></a>
               </div>
                 <div id="searcharea"><form method="get" accept-charset="iso-8859-1" action="http://www.javaworld.com/ifind/java/query.html"><input type=hidden name="qm" value="0" />
<input type=hidden name="st" value="1" />
<input type=hidden name="nh" value="10" />
<input type="hidden" name="col" value="java" />
  <table border="0" cellspacing="0" cellpadding="0">
  <tr valign="top">
    <td><input type="text" name="qt" class="formfield" value="" maxlength="1991"></td>
    <td><input class="submit" name="" type="image" src="/includes/styles/i/but/lw-search-button.gif" alt="Search" value="Go" /></td>
    <td><span><a href="http://www.javaworld.com/ifind/java/query.html?col=java&qs=&qc=&si=0&ql=a&nh=25&lk=1&rf=0">Advanced search</a></span></td>
  </tr>
</table>
  </form>
</div>

            </div>
            <div class="separator">
               <!-- xxxxxxxxxxxxxx -->
            </div>
         </div>
         <!--Start Menu--><script language="javascript" src="/includes/lookup_topic.js" xml:space="preserve"> </script><div id="toolbar"><img height="15" src="/images/jw-logo-tagline.gif" width="125"/><div id="medallion"><!-- begin ad tag -->

<table width="125"  border="0" cellspacing="0" cellpadding="0"><tr><td><SCRIPT LANGUAGE="JavaScript1.1" SRC="http://ad.doubleclick.net/adj/idg.us.nwf.jw_general/;pos=n1;sz=120x90;ptile=2;type=news;ord=023131?" ></SCRIPT></td></tr></table>
<script>
if ((!document.images && navigator.userAgent.indexOf("Mozilla/2.") >= 0)  || navigator.userAgent.indexOf("WebTV")>= 0) {
document.write('<A HREF="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;pos=n1;sz=120x90;ptile=2;type=news;ord=023131?" TARGET="_blank">');
document.write('<IMG SRC="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;pos=n1;sz=120x90;ptile=2;type=news;ord=023131?" WIDTH="120" HEIGHT="90" BORDER="0" ALT="Click Here"></A>');
}
</script>
<NOSCRIPT>
<A HREF="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;pos=n1;sz=120x90;ptile=2;type=news;ord=023131?" TARGET="_blank">
<IMG SRC="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;pos=n1;sz=120x90;ptile=2;type=news;ord=023131?" WIDTH="120" HEIGHT="90" BORDER="0" ALT="alt text">Click Here</A>
</NOSCRIPT>
<!-- end ad tag --><div></div></div><div id="toolbarhome"><ul><li id="hmbar" class=""><a href="/index.html" shape="rect">Home</a></li></ul></div><ul><li class="research"><a href="/topics/index.html" shape="rect">Research Centers</a></li></ul><div id="toolbar_rc"><ul class="botbar"><li id="navbar1" class="nobracket"><a shape="rect" id="linknav1" href="/channel_content/jw-core-index.html" class=""><span id="showplus1" class="toolbar-red">
			+ </span>Java Standard Edition</a><div id="toolbar_rcsubs1"><ul><li id="subnav98" class=""><a shape="rect" href="/channel_content/jw-apis-index.html">APIs</a></li><li id="subnav105" class=""><a shape="rect" href="/channel_content/jw-awt-index.html">AWT / Swing</a></li><li id="subnav108" class=""><a shape="rect" href="/channel_content/jw-patterns-index.html">Design Patterns</a></li><li id="subnav37" class=""><a shape="rect" href="/channel_content/jw-ui-index.html">User Interface Design</a></li><li id="subnav13" class=""><a shape="rect" href="/channel_content/jw-jdbc-index.html">Java Database Connectivity (JDBC)</a></li><li id="subnav103" class=""><a shape="rect" href="/channel_content/jw-jvm-index.html">Java Virtual Machine (JVM)</a></li><li id="subnav65" class=""><a shape="rect" href="/columns/jw-qna-index.html">Java Q&amp;A</a></li><li id="subnav57" class=""><a shape="rect" href="/columns/jw-tips-index.html">Java Tips</a></li></ul></div></li><li id="navbar2" class="nobracket"><a shape="rect" id="linknav2" href="/channel_content/jw-enterprise-index.html" class=""><span id="showplus2" class="toolbar-red">
			+ </span>Java Enterprise Edition</a><div id="toolbar_rcsubs2"><ul><li id="subnav8" class=""><a shape="rect" href="/channel_content/jw-ejbs-index.html">Enterprise JavaBeans (EJBs)</a></li><li id="subnav16" class=""><a shape="rect" href="/channel_content/jw-message-index.html">Java Message Service</a></li><li id="subnav14" class=""><a shape="rect" href="/channel_content/jw-jsp-index.html">JavaServer Pages</a></li><li id="subnav19" class=""><a shape="rect" href="/channel_content/jw-servlets-index.html">Servlets</a></li><li id="subnav9" class=""><a shape="rect" href="/channel_content/jw-webserv-index.html">Web Services and SOAs</a></li><li id="subnav10" class=""><a shape="rect" href="/channel_content/jw-xml-index.html">Java and XML</a></li><li id="subnav30" class=""><a shape="rect" href="/channel_content/jw-design-index.html">Design Patterns</a></li><li id="subnav31" class=""><a shape="rect" href="/channel_content/jw-performance-index.html">Performance Tuning</a></li></ul></div></li><li id="navbar3" class="nobracket"><a shape="rect" id="linknav3" href="/channel_content/jw-micro-index.html" class=""><span id="showplus3" class="toolbar-red">
			+ </span>Java Micro Edition</a><div id="toolbar_rcsubs3"><ul/></div></li><li id="navbar4" class="nobracket"><a shape="rect" id="linknav4" href="/channel_content/jw-applied-index.html" class=""><span id="showplus4" class="toolbar-red">
			+ </span>Development Tools</a><div id="toolbar_rcsubs4"><ul><li id="subnav38" class=""><a shape="rect" href="/channel_content/jw-amt-index.html">Application Management</a></li><li id="subnav41" class=""><a shape="rect" href="/channel_content/jw-dataaccesstools-index.html">Data Access Tools</a></li><li id="subnav44" class=""><a shape="rect" href="/channel_content/jw-gamingtools-index.html">Gaming Tools</a></li><li id="subnav21" class=""><a shape="rect" href="/channel_content/jw-webdev-index.html">Web Development Frameworks</a></li><li id="subnav110" class=""><a shape="rect" href="/channel_content/jw-security-testing-index.html">Security &amp; Testing</a></li><li id="subnav12" class=""><a shape="rect" href="/channel_content/jw-appserv-index.html">Java Application Servers</a></li><li id="subnav48" class=""><a shape="rect" href="/channel_content/jw-profilingtools-index.html">Profiling and Monitoring</a></li><li id="subnav49" class=""><a shape="rect" href="/channel_content/jw-reportingtools-index.html">Reporting</a></li></ul></div></li></ul></div> <!-- use li class=current1 to hightlight the items below-->
    <ul class="botbar">
      <li class="toplevel2"><a href="/resources/site-resources.html">Site Resources</a></li>
      <li id="sr87" class=""><a href="/features/index.html">Featured Tutorials</a></li>
	  <li id="sr89" class=""><a href="/news-reviews/index.html">News &amp; Reviews</a></li>
	  <li id="sr990" class=""><a href="/javaforums/ubbthreads.php?Cat=2&C=2">Forums</a></li>
	  <li id="sr993" class=""><a href="/podcasts/jtech/">Podcasts</a></li>
      <li id="sr991" class=""><a href="/newsletter/">Newsletters</a></li>
      <li id="sr95" class=""><a href="/white-paper/index.html">White Paper Library</a></li>
      <li id="sr199" class=""><a href="/resources/index.html">Web resources</a></li>
      <li id="sr992" class=""><a href="/rss/index.html">RSS Feeds</a></li>
    </ul>
	 <ul class="botbar">
      <li class="toplevel2"><a href="http://javaworld.4jobs.com">Careers</a></li>
    </ul>
    <div class="toolspace">
      <!--3px of space-->
    </div>
    <ul class="botbar">
      <li class="toplevel2"><a href="/partners.html">Partner Sites</a></li>
      <li><a href="http://www.demo.com">Demo.com</a></li>
	  <li><a href="http://www.linuxworld.com">LinuxWorld.com</a></li>
      <li><a href="http://www.networkworld.com">NetworkWorld.com</a></li>  
    </ul>
	<ul><li class="toplevel2"><a href="/info/jw-about-index.html">About Us</a></li>
	</ul>
	<script language="javascript">if (typeof menu_lookup == "function") menu_lookup();</script>
	
	<br>
	<div id="techwords" style="background-color:#FFFFFF;">
	<h6>sponsored links</h6>
		<a href="http://www.techwords.com/">See your link here.</a>
		<script type="text/javascript">
		adsonar_placementId=1268728;
		adsonar_pid=462761;
		adsonar_ps=1424789;
		adsonar_zw=120;
		adsonar_zh=600;
		adsonar_jv='ads.adsonar.com';
		</script>
		<script language="JavaScript" src="http://js.adsonar.com/js/adsonar.js"></script>
	</div>
	
	</div>
         <div class="rc" id="contentarea">
            <div id="rcheader">
               <!--startindex-->
               <div id="toptabs">
                  <ul>
                     <li><a href="/channel_content/jw-general-index.html"></a></li>
                  </ul>
               </div>
               <!--stopindex-->
            </div>
            <div id="idglogin">
               <!-- xxxxxxxxxxxxxx -->
            </div>
            <div id="sectionhome">
               <div id="a-e_col1">
                  <div id="d-e_col2b">
                     <div id="d-e-1">
	<div id="tabs">
		<ul>
		<li class="ts"><a href="javascript:opentab('topstories');" class="ts" id="topstories_a">Best of JavaWorld</a></li>
		<li class="saved"><a href="javascript:opentab('savedarticles');" class="saved" id="savedarticles_a">Editor's Choice</a></li>
		</ul>
	</div>
	<div id="topstories">
		 <p xmlns:o="urn:www.microsoft.com/office" xmlns:st1="urn:www.microsoft.com/smarttags" xmlns:w="urn:www.microsoft.com/word"><a href="http://www.javaworld.com/podcasts/jtech/2007/112007jtech006.html">Neal Ford on what JRuby has the Java doesn't</a><br>Find out why more Java developers are migrating to dynamic languages and what this means for the future of the Java platform.
   Part of JavaWorld's new podcast series presenting in-depth discussions with some of the most interesting thinkers working
   on the Java platform today.
</p>
	</div>
	<div id="savedarticles">
		 <p xmlns:o="urn:www.microsoft.com/office" xmlns:st1="urn:www.microsoft.com/smarttags" xmlns:w="urn:www.microsoft.com/word"><b>From the Network World Data Center:</b></p>
<p xmlns:o="urn:www.microsoft.com/office" xmlns:st1="urn:www.microsoft.com/smarttags" xmlns:w="urn:www.microsoft.com/word"><a href="http://www.networkworld.com/supp/2007/ndc6/102207-soa-user-case-studies.html">Inside three enterprise SOAs</a><br><a href="http://www.networkworld.com/supp/2007/ndc6/102207-soa-management-guide.html">What SOA means for management</a><br><a href="http://www.networkworld.com/supp/2007/ndc1/021907-ndc-best-of-enterprise-soa.html">10 best practices for your SOA</a><br><a href="http://www.networkworld.com/supp/2007/ndc6/102207-services-oriented-infrastructure.html">SOA, meet SOI</a></p>
	</div>
</div>

<script type="text/javascript">
divnames = new Array('topstories', 'savedarticles'); <!--,'topstories'-->
opentab("savedarticles");

function opentab(divname) {	;
	if(document.getElementById(divname)) {
		for (i=0;i < 2;i++) {	<!-- 2-->
			var aname = divnames[i] + '_a';		
			if (divnames[i] == divname)	{	
				tab=divname;		    
				document.getElementById(divname).style.display='block';
				document.getElementById(aname).parentNode.id = "current";	
			}
			else {
				document.getElementById(divnames[i]).style.display='none';
				document.getElementById(aname).parentNode.id = "none";
			}
		}
	}
}
</script>


                     <div class="imu">
                        <!--stopindex-->







  

<script language="JavaScript">
<!--
var imuvar;
myImage = new Image();
myImagef = new Image();
function resize(){
	//return; //just comment this out to activate the resize function
	imuvar = document.getElementById("imu");
	myImage.src = "http://www.javaworld.com/ads/halfpage.jsp?get=true&ord=023131";
	setTimeout("getSize()",300);
	setTimeout("finalSize()",1200);
}

function finalSize(){
	if (imuvar.height < 600) {
		imuvar = document.getElementById("imu");
		myImagef.src = "http://www.javaworld.com/ads/halfpage.jsp?get=true&ord=1023131";
		getSizef();
	}
}

function getSizef() {
    if (myImagef.height == 0) setTimeout("getSizef()",200);	
	if (myImagef.height == 600) imuvar.height=600;
}

function getSize() {
    if (myImage.height == 0) setTimeout("getSize()",200);	
	if (myImage.height == 600) imuvar.height=600;
}
//-->
</script>
  
  <NOLAYER>
  <IFRAME id="imu" src="http://ad.doubleclick.net/adi/idg.us.nwf.jw_general/;pos=imu;sz=336x280;ptile=3;type=news;ord=023131?" onload="resize()" width="336" height="280" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no">
  <A href="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;abr=!ie;pos=imu;sz=336x280;ptile=3;type=news;ord=023131?">
  <IMG src="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;abr=!ie;pos=imu;sz=336x280;ptile=3;type=news;ord=023131?" border="0" width="336" height="280"></A>
  </IFRAME>
  </NOLAYER>
  <ILAYER id="imu" visibility="hidden" width="336" height="280"></ILAYER>
	<script> imusrc = "http://ad.doubleclick.net/adi/idg.us.nwf.jw_general/;pos=imu;sz=336x280;ptile=3;type=news;ord=";</script>

                     </div>
                     
<div id="rclib-de1">
   <h5 class="rclibde">
      <b>Featured Whitepapers</b>
   </h5>
   <table border="0" cellpadding="0" cellspacing="0" class="rclib-detable" width="100%">
      <tr>
         <td class="rclib-dealt">
            <a href=""/>
         </td>
      </tr>
   </table>
</div>
                             <!-- nl signup start-->
		<form action="http://www.subscribenw.com/nl/Signup.do" id="Form2" method="post" name="form" onSubmit="openNewWindow()" target="new_window">
          <div id="newsletter">
            <h5>Newsletter sign-up</h5>
			<p class="nl">Sign up for our technology specific newsletters.</p>
            <table width="300" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="27" valign="top" class="bgleft"><input name="NL70" type="checkbox" id="NL70"/></td>
                <td width="265" class="bgright"><a href="javascript:openWindow('http://www.subscribenw.com/nl/popup.jsp?id=70',500,300);">Enterprise Java</a></td>
              </tr>
			  <tr><td colspan="2"><a href="http://www.subscribenw.com/nl/signup.jsp?keycode=JW07">View all newsletters</a></td></tr>
		    </table>
            <hr color="#CCCCCC" noshade="noshade" size="1"/>
            <strong>Email Address:</strong>
			<input id="keycode" name="keycode" type="hidden" value="NLmsu08"/>
			<input id="call" name="call" type="hidden" value="instant"/>
			<input name="pid" type="hidden" value="121"/>
			<input name="email" size="14" type="text"/>
            <input alt="Submit" name="Button" type="submit" value="Subscribe" id="Submit1"/>
          </div>
        </form>

                     <!--include virtual="/includes/topic/general-nl-signup.html"-->
                     
<div id="de5">
<!-- begin ad tag -->
<SCRIPT LANGUAGE="JavaScript1.1" SRC="http://ad.doubleclick.net/adj/idg.us.nwf.jw_general/;pos=de5;sz=336x175;tile=5;ord=023131?" ></SCRIPT>
<script>
if ((!document.images && navigator.userAgent.indexOf("Mozilla/2.") >= 0)  || navigator.userAgent.indexOf("WebTV")>= 0) {
document.write('<A HREF="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;pos=de5;sz=336x175;tile=5;ord=023131?" TARGET="_blank">');
document.write('<IMG SRC="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;pos=de5;sz=336x175;tile=5;ord=023131?" WIDTH="336" HEIGHT="175" BORDER="0" ALT="Click Here"></A>');
}
</script>
<NOSCRIPT>
<A HREF="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;pos=de5;sz=336x175;tile=5;ord=023131?" TARGET="_blank">
<IMG SRC="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;pos=de5;sz=336x175;tile=5;ord=023131?" WIDTH="336" HEIGHT="175" BORDER="0" ALT="alt text">Click Here</A>
</NOSCRIPT>
<!-- end ad tag -->
</div>

                     <div id="de6">
   <h5>Sponsored Links</h5>
   <p><a href="http://links.industrybrains.com/click?sid=620&amp;rqctid=5123&amp;pos=1&amp;lid=463656&amp;cid=131414&amp;pr=2&amp;tstamp=20071129021513&amp;url=http://store.cablesplususa.com/fiber-optic-patch-cables.html%3fovchn%3dGGL%26ovcpn%3dIndustry%2bBrains%26ovcrn%3dFiber%2bOptic%26ovtac%3dPPC" target=_blank>10% Off Fiber Patch Cables SALE &amp; So Much More!</a><br>Enter coupon code FIBER10OFF at checkout and enjoy FREE On-line chat support with fast delivery!
   </p>
</div>
                  </div>
                  <div class="breadcr"><a href="/index.html">JavaWorld.com</a>
                     <!-- SSI Error --></div>
                  <div>
                     
                     <!--HEADLINE GOES HERE-->
                     
                     <!--startindex-->
                     <a id="top" name="top"></a><h1>Java Tip 17: Integrating Java with C++</h1>
                     <H3>&lt;H3&gt;Learn how to use C++ code from within a Java application and how to call from C++ to a Java object&lt;/H3&gt;&lt;/CENTER&gt;</H3>
                     <p class="byline">By&nbsp;Bill Foote,&nbsp;JavaWorld.com,&nbsp;09/01/96</p>
                     
                     <!-- CONTENT GOES HERE-->
                     In this article, I'll discuss some of the issues involved in integrating C++ code with a Java application. After a word about
                     why one would want to do this and what some of the hurdles are, I'll build up a working Java program that uses objects written
                     in C++. Along the way, I'll discuss some of the implications of doing this (such as interaction with garbage collection),
                     and I'll present a glimpse of what we can expect in this area in the future.<br><br> 
                     <h3>Why integrate C++ and Java?</h3>
                     <p class="first">Why would you want to integrate C++ code into a Java program in the first place? After all, the Java language was created,
                        in part, to address some of the shortcomings of C++. Actually, there are several reasons why you might want to integrate C++
                        with Java:
                     </p>
                     <ul>
                        <li>Performance. Even if you're developing for a platform with a just-in-time (JIT) compiler, odds are that the code generated
                           by the JIT runtime is significantly slower than the equivalent C++ code. As JIT technology improves, this should become less
                           of a factor. (In fact, in the near future, good JIT technology may well mean that Java runs <em>faster</em> than the equivalent C++ code.)
                        </li>
                        <li>For reuse of legacy code and integration into legacy systems.</li>
                        <li>To directly access hardware or do other low-level activities.</li>
                        <li>To leverage tools that are not yet available for Java (mature OODBMSes, ANTLR, and so on).</li>
                     </ul><br><br> 
                     <p>If you take the plunge and decide to integrate Java and C++, you do give up some of the important advantages of a Java-only
                        application. Here are the downsides:
                     </p>
                     <ul>
                        <li>A mixed C++/Java application cannot run as an applet.</li>
                        <li>You give up pointer safety. Your C++ code is free to miscast objects, access a deleted object, or corrupt memory in any of
                           the other ways that are so easy in C++.
                        </li>
                        <li>Your code may not be portable.</li>
                        <li>Your built environment definitely won't be portable -- you'll have to figure out how to put C++ code in a shared library on
                           all platforms of interest.
                        </li>
                        <li>The APIs for integrating C and Java are works in progress and will very likely change with the move from JDK 1.0.2 to JDK
                           1.1.
                        </li>
                     </ul><br><br> 
                     <p>As you can see, integrating Java and C++ is not for the faint of heart! However, if you wish to proceed, read on.</p>
                     <p>We'll start with a simple example showing how to call C++ methods from Java. We'll then extend this example to show how to
                        support the observer pattern. The observer pattern, in addition to being one of the cornerstones of object-oriented programming,
                        serves as a nice example of the more involved aspects of integrating C++ and Java code. We'll then build a small program to
                        test our Java-wrapped C++ object, and we'll end with a discussion of future directions for Java.
                     </p>
                     <h3>Calling C++ from Java</h3>
                     <p>What's so hard about integrating Java and C++, you ask? After all, SunSoft's <em>Java Tutorial</em> has a section on "Integrating Native Methods into Java Programs" (see <a href="#Resources">Resources</a>). As we'll see, this is adequate for calling C++ methods from Java, but it doesn't give us enough to call Java methods from
                        C++. To do that, we'll need to do a little more work.
                     </p>
                     <p>As an example, we'll take a simple C++ class that we'd like to use from within Java. We'll assume that this class already
                        exists and that we're not allowed to change it. This class is called "C++::NumberList" (for clarity, I'll prefix all C++ class
                        names with "C++::"). This class implements a simple list of numbers, with methods to add a number to the list, query the size
                        of the list, and get an element from the list. We'll make a Java class whose job it is to represent the C++ class. This Java
                        class, which we'll call NumberListProxy, will have the same three methods, but the implementation of these methods will be
                        to call the C++ equivalents. This is pictured in the following object modeling technique (OMT) diagram:
                        &nbsp;&nbsp;<a href="?page=2">Continued</a>
                     </p>									 
									 	 <div id="pagenum"><hr size="1">1 | <a href="/javaworld/javatips/jw-javatip17.html?page=2" class="noline"><strong>2</strong></a> | <a href="/javaworld/javatips/jw-javatip17.html?page=3" class="noline"><strong>3</strong></a> | <a href="/javaworld/javatips/jw-javatip17.html?page=4" class="noline"><strong>4</strong></a> | <a href="/javaworld/javatips/jw-javatip17.html?page=5" class="noline"><strong>5</strong></a> | <a href="/javaworld/javatips/jw-javatip17.html?page=6" class="noline"><strong>6</strong></a> | &nbsp;<a href="/javaworld/javatips/jw-javatip17.html?page=2" class="noline"><strong>Next&nbsp;></strong></a></div>
									 
                     
                     <!-- CONTENT ENDS HERE -->
                     
                     <!--stopindex-->
                     
                     
<!-- mypages:  -->
<!--story tools start-->
<script>
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
var test = "/adtran/solutionDetails.jsp?subject="
eval(targ+".location='"+test+"'+'"+selObj.options[selObj.selectedIndex].value+"'")
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>
				<div id="storytools">
	      			<ul>
					  	<form method="post" name="print" action="/cgi-bin/mailto/x_java.cgi">
<input type="hidden" name="pagetosend" value="/export/home/httpd/javaworld/javaworld/javatips/jw-javatip17.html" />
<input type="hidden" name="pagename" value="/javaworld/javatips/jw-javatip17.html" />
<input type="hidden" name="pageurl" value="http://www.javaworld.com/javaworld/javatips/jw-javatip17.html" />
<input type="hidden" name="site" value="jw_general" /><li><input type="image" title="print" src="/includes/styles/i/ico/print06-icon.gif" class="printer" alt="print" width="18" height="13" border="0" />
						<a href="javascript:document.forms.print.submit();">Print</a>
						</li> 
						</form>
						
	 <!-- 
						<li><img src="/includes/styles/i/ico/email-06-icon.gif" width="17" height="13" /><a href="javascript:;" onClick="MM_openBrWindow('/mailto/mailform.jsp?refpage=jw&pagetosend=/export/home/httpd/javaworld/javaworld/javatips/jw-javatip17.html&pagename=/javaworld/javatips/jw-javatip17.html&pageurl=/javaworld/javatips/jw-javatip17.html','Registration','scrollbars=yes,resizable=yes,width=510,height=500')">E-Mail article </a></li>
--> 

<li><img src="/includes/styles/i/ico/email-06-icon.gif" width="17" height="13" /><a href="javascript:;" onClick="MM_openBrWindow('/storytools/send_to_friend-form.jsp?rxid=83833&refpage=jw&pagetosend=/export/home/httpd/javaworld/javaworld/javatips/jw-javatip17.html&pagename=/javaworld/javatips/jw-javatip17.html&pageurl=/javaworld/javatips/jw-javatip17.html','Registration','scrollbars=yes,resizable=yes,width=625,height=480')">E-Mail article </a></li>
	 
						<li><img src="/includes/styles/i/ico/fdbck06-icon.gif" width="14" height="13" /><a href="javascript:;" onClick="MM_openBrWindow('/askTheExpert.jsp?pagename=http://www.javaworld.com/javaworld/javatips/jw-javatip17.html&pagename=/javaworld/javatips/jw-javatip17.html&pageurl=/javaworld/javatips/jw-javatip17.html&pubsite=j','Registration','scrollbars=yes,resizable=yes,width=500,height=600')">Feedback</a></li>
						<li><img src="/includes/styles/i/ico/add-icon.gif" width="14" height="14" /><a href="javascript:location.href='http://del.icio.us/post?v=4;url='+encodeURIComponent(location.href)+';title='+encodeURIComponent(document.title)">Add to del.icio.us</a></li>
						<li class="next"><script src="/includes/nextlink.js"></script></li>						
					</ul>
				</div>
   <div class="separatorzero">
          <!-- xxxxxxxxxxxxxx -->
        </div>
        <!--story tools end--><a name="resources"></a><strong>Resources</strong><br><externallinks no-escaping="yes">
                        <div class="rxbodyfield">
                           <ul>
                              <li>For information on ANTLR, see<br><a href="http://www.mcs.com/~tmoog/pccts.html">http://www.mcs.com/~tmoog/pccts.html</a></li>
                              <li>SunSoft's Java Tutorial is available at<br><a href="http://www.javasoft.com/books/Series/Tutorial/TOC.html">http://www.javasoft.com/books/Series/Tutorial/TOC.html</a></li>
                              <li>See the Java Tutorial section on "Integrating Native Methods into Java Programs"<br><a href="http://www.javasoft.com/books/Series/Tutorial/native/index.html">http://www.javasoft.com/books/Series/Tutorial/native/index.html</a></li>
                              <li>See JavaSoft's announcement that JDK 1.1 will have a new Java native method interface<br><a href="http://www.javasoft.com/products/JDK/1.1/designspecs/index.html">http://www.javasoft.com/products/JDK/1.1/designspecs/index.html</a></li>
                              <li>Netscape's documentation on their JRI native method interface can be found here<br><a href="http://home.netscape.com/eng/jri/jri-native.html">http://home.netscape.com/eng/jri/jri-native.html</a></li>
                              <li>For ILOG's home page, see<br><a href="http://www.ilog.com">http://www.ilog.com</a></li>
                              <li>ILOG's TwinPeaks project is described at their site<br><a href="http://www.ilog.com/ilog/press/english/twinpeaks.html">http://www.ilog.com/ilog/press/english/twinpeaks.html</a></li>
                           </ul>
                        </div>
                     </externallinks><br>
                     
</div>
               </div>
               

<br clear="all"/>
 
<!-- begin ad tag -->
<SCRIPT LANGUAGE="JavaScript1.1" SRC="http://ad.doubleclick.net/adj/idg.us.nwf.jw_general/;pos=bottom;sz=120x90;tile=6;ord=023131?" ></SCRIPT>
<script>
if ((!document.images && navigator.userAgent.indexOf("Mozilla/2.") >= 0)  || navigator.userAgent.indexOf("WebTV")>= 0) {
document.write('<A HREF="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;pos=bottom;sz=120x90;tile=6;ord=023131?" TARGET="_blank">');
document.write('<IMG SRC="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;pos=bottom;sz=120x90;tile=6;ord=023131?" WIDTH="120" HEIGHT="90" BORDER="0" ALT="Click Here"></A>');
}
</script>
<NOSCRIPT>
<A HREF="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;pos=bottom;sz=120x90;tile=6;ord=023131?" TARGET="_blank">
<IMG SRC="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;pos=bottom;sz=120x90;tile=6;ord=023131?" WIDTH="120" HEIGHT="90" BORDER="0" ALT="alt text">Click Here</A>
</NOSCRIPT>
<!-- end ad tag -->

               <div class="separator">
                  <!-- xxxxxxxxxxxxxx -->
               </div>
               
               <div id="sponsoredlinks">
	<h5>Sponsored links</h5>
	<DIV id="toplinks">&nbsp;</DIV>
<DIV id="middlelinks">
   <TABLE cellPadding="0" cellSpacing="0" id="nwtextlinks">
      <TBODY>
         <tr>
            <td>
               <a href="http://ad.doubleclick.net/clk;151501168;12174868;e?http://www.agitar.com/javacoverage?utm_source=javaworld&amp;utm_medium=textlink&amp;utm_content=coverage&amp;utm_campaign=javaworld-coverage-sept-2007" title="Agitar Software" onclick="" target="_blank" name="&amp;lid=rid_116776 1318592 Agitar November JW Text Link">80% Java Test Coverage! Now guaranteed with automated JUnit test generation. Free trial!</a>
               <strong>- Agitar Software</strong>
            </td><td>&nbsp;</td></tr>
      </TBODY>
   </TABLE>
</DIV>
	<div id="bottomlinks">
		<H4><A href="http://www.industrybrains.com/networkworld/Default.aspx">Buy a link now.</A></H4>
<p style="width:400px;"><a href="http://links.industrybrains.com/click?sid=620&amp;rqctid=5123&amp;pos=1&amp;lid=444646&amp;cid=101974&amp;pr=2&amp;tstamp=20071129023034&amp;url=http://www.webex.com/go/IB_1008156" target="blank">Free Trial Signup: Access your PC from any device.</a><br>Access your remote PCs from any web browser or mobile device. 30-day free trial!  (CC Required).
</p>
<p style="width:400px;"><a href="http://links.industrybrains.com/click?sid=620&amp;rqctid=5123&amp;pos=2&amp;lid=463374&amp;cid=131402&amp;pr=2&amp;tstamp=20071129023034&amp;url=http://www2.itt-tech.edu/st10/%3fcode%3dindustry%2bbrains%2b%7c%2bContextual%2bTargeting%26sendroicid%3d12ae02c4-5136-4ebf-86c0-974a47485edc%26sendroikwd%3dJavaworld%2b-%2bHP%2b%2526%2bROS" target="blank">IT Developer Training</a><br>Begin preparing for your career in Information Technology at ITT Tech.
</p>
<p style="width:400px;"><a href="http://links.industrybrains.com/click?sid=620&amp;rqctid=5123&amp;pos=3&amp;lid=462685&amp;cid=128034&amp;pr=2&amp;tstamp=20071129023034&amp;url=http://ad.doubleclick.net/clk%3b144375816%3b20850329%3bh%3fhttp://info.vmware.com/content/LabMgrLP%3fsrce%3dstxt_07Q4IB_textlink%26ossrc%3dstxt_07Q4IB" target="blank">Virtualize the test lab. VMWare Lab Manager</a><br>Sign up here for your Free trial of VMware Lab Manager and try it today!
</p>
		<!--<H4><A href="http://www.industrybrains.com/">Buy a link now.</A></H4><br clear="all">
		<script type="text/javascript">
		adsonar_pid=462761; // this is the original javaworld pid
		adsonar_ps=1061721;
		adsonar_zw=460;
		adsonar_zh=190;
		adsonar_jv='ads.adsonar.com';
		</script>
		<script language="JavaScript" src="http://js.adsonar.com/js/adsonar.js"></script>	 -->
		<br clear="all">
	</div>
</div>
               <div class="separator">
                  <!-- xxxxxxxxxxxxxx -->
               </div>
               <div id="rcfooter"><strong>RESEARCH CENTERS: </strong>
<a href="http://www.javaworld.com/channel_content/jw-core-index.html"><strong>Java Standard Edition</strong></a> | 
<a href="http://www.javaworld.com/channel_content/jw-enterprise-index.html"><strong>Java Enterprise Edition</strong></a> | 
<a href="http://www.javaworld.com/channel_content/jw-micro-index.html"><strong>Java Micro Edition </strong></a> | 
<a href="http://www.javaworld.com/channel_content/jw-applied-index.html"><strong>Development Tools</strong></a>

<div align="center"><a href="http://www.javaworld.com/info/jw-about-index.html">About Us</a> | <a href="http://www.javaworld.com/info/jw-advertise.html">Advertise</a> | <a href="http://www.javaworld.com/info/jw-masthead.html">Contact Us</a> | <a href="http://www.javaworld.com/info/jw-privacy.html">Terms of Service/Privacy</a> </div>
<div align="center"><a href="http://www.javaworld.com/javaworld/common/jw-copyright.html">Copyright</a>, 2006-2007 Network World, Inc. All rights reserved.</div>
</div>
            </div>
            <div id="footer"><span><a href="http://www.idg.net"><strong>IDG Network:</strong></a> 
<a href="http://www.cio.com">CIO</a>  
<a href="http://www.computerworld.com">Computerworld</a> <a href="http://www.csoonline.com/">CSO</a> 
<a href="http://demo.com/">Demo</a> 
<a href="http://www.gamestar.com/">GamePro</a> 
<a href="http://www.games.net/">Games.net</a> 
<a href="http://www.idgconnect.com">IDGconnect.com</a> 
<a href="http://www.idgworldexpo.com/">IDG World Expo</a>
<a href="http://www.infoworld.com">Infoworld</a> 
<a href="http://www.linuxworld.com">Linuxworld.com</a> 
<a href="http://www.macuser.com">MacUser</a> 
<a href="http://www.macworld.com">Macworld</a> 
<a href="http://www.networkworld.com">NetworkWorld.com</a> 
<a href="http://www.pcworld.com">PC World</a> 
<a href="http://www.playlistmag.com">Playlistmag.com</a></span></div>
<!-- start Vibrant Media IntelliTXT script section -->

<script type="text/javascript" src="http://javaworld.us.intellitxt.com/intellitxt/front.asp?ipid=3407"></script>

<script language="javascript">
	if (typeof menu_lookup == "function") menu_lookup();
</script>
<!-- end Vibrant Media IntelliTXT script section -->
<!-- begin ad tag  (tile=99) -->
<script language="JavaScript" type="text/javascript">
ord=Math.random()*10000000000000000;
document.write('<script language="JavaScript" src="http://ad.doubleclick.net/adj/idg.us.nwf.jw_general/;tile=99;dcopt=ist;sz=1x1;ord=' + ord + '?" type="text/javascript"><\/script>');
</script><script language="JavaScript" type="text/javascript">if ((!document.images && navigator.userAgent.indexOf('Mozilla/2.') >= 0)|| navigator.userAgent.indexOf("WebTV") >= 0) {document.write('<a href="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;tile=99;sz=1x1;ord=' + ord + '?" target="_blank"><img src="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;tile=99;sz=1x1;ord=' + ord + '?" width="1" height="1" border="0" alt=""><\/a>');}</script><noscript><a href="http://ad.doubleclick.net/jump/idg.us.nwf.jw_general/;tile=99;sz=1x1;ord=' + ord + '?" target="_blank"><img src="http://ad.doubleclick.net/ad/idg.us.nwf.jw_general/;tile=99;sz=1x1;ord=' + ord + '?" width="1" height="1" border="0" alt=""></a></noscript>
<!-- End ad tag -->


         </div>
      </div>
   </body>
</html>